package com.liuzhilong.infrastructure.demo.state.machine.spring.simpleleave.config;

import com.liuzhilong.infrastructure.demo.state.machine.spring.simpleleave.definiton.LeaveApplyEvent;
import com.liuzhilong.infrastructure.demo.state.machine.spring.simpleleave.definiton.LeaveApplyState;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;

import javax.annotation.PostConstruct;
import java.util.EnumSet;

@Configuration
@EnableStateMachine
public class LeaveStateMachineConfig extends StateMachineConfigurerAdapter<LeaveApplyState, LeaveApplyEvent> {
    @PostConstruct
    public void init() {
        System.out.println("init");
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<LeaveApplyState, LeaveApplyEvent> config)
            throws Exception {
        config
                .withConfiguration()
                .autoStartup(true)
                .listener(listener());
    }
    /**
     * 定义状态机初始化的时候的状态
     * @param states
     * @throws Exception
     */
    @Override
    public void configure(StateMachineStateConfigurer<LeaveApplyState, LeaveApplyEvent> states)
            throws Exception {
        states
                .withStates()
                .initial(LeaveApplyState.INIT)
                .states(EnumSet.allOf(LeaveApplyState.class));
    }

    /**
     * 定义各个状态机的状态迁移表
     * @param transitions
     * @throws Exception
     */
    @Override
    public void configure(StateMachineTransitionConfigurer<LeaveApplyState, LeaveApplyEvent> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(LeaveApplyState.INIT).target(LeaveApplyState.MANAGER)
                .event(LeaveApplyEvent.SUBMIT)

                .and()
                .withExternal()
                .source(LeaveApplyState.MANAGER).target(LeaveApplyState.BOSS)
                .event(LeaveApplyEvent.PASS)

                .and()
                .withExternal()
                .source(LeaveApplyState.MANAGER).target(LeaveApplyState.REFUSED)
                .event(LeaveApplyEvent.REJECT)

                .and()
                .withExternal()
                .source(LeaveApplyState.BOSS).target(LeaveApplyState.FINISH)
                .event(LeaveApplyEvent.PASS)
                .and()
                .withExternal()
                .source(LeaveApplyState.BOSS).target(LeaveApplyState.REFUSED)
                .event(LeaveApplyEvent.REJECT)

                .and()
                .withExternal()
                .source(LeaveApplyState.REFUSED).target(LeaveApplyState.MANAGER)
                .event(LeaveApplyEvent.SUBMIT)


                .and()
                .withExternal()
                .source(LeaveApplyState.REFUSED).target(LeaveApplyState.CLOSED)
                .event(LeaveApplyEvent.CLOSE)

                .and()
                .withExternal()
                .source(LeaveApplyState.FINISH).target(LeaveApplyState.CLOSED)
                .event(LeaveApplyEvent.CLOSE)
        ;


    }
    /**
     * 配置 事件监听器
     * @return
     */
    @Bean
    public StateMachineListener<LeaveApplyState, LeaveApplyEvent> listener() {
        return new StateMachineListenerAdapter<LeaveApplyState, LeaveApplyEvent>() {
            @Override
            public void stateChanged(State<LeaveApplyState, LeaveApplyEvent> from, State<LeaveApplyState, LeaveApplyEvent> to) {
                if(from==null){
                    System.out.println("State change to " + to.getId());
                }else {
                    System.out.println("State change from "+from.getId()+" to " + to.getId());

                }
            }
        };
    }




}
